<!--* Created by caiqi on 16/12/19.-->
<template>
  <div class="docu">
    <van-tabs @change="muneSelect" v-model="select_trademar">
      <van-tab v-for="(item) in trademarkList" :key="item._id" :title="item.name"></van-tab>
    </van-tabs>

    <van-button type="default" class="Uwant" size="small" @click="dialogVisible=true">你想知道什么？</van-button>

    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="getDocList"
      :offset="10"
      class="doc-list"
    >
      <div v-for="(item) in docList" :key="item._id" class="item" @click="toDetail(item._id)">
        <div :style="{'background-image':`url(${item.surface})`}" class="image"></div>
        <div class="info">
          <div>{{item.title}}</div>
          <div class="readSize">
            阅读：
            {{item.readSize||0}}
            <br>
            喜欢：
            {{item.likeSize||0}}
          </div>
          <div class="time">{{ item.created_date }}</div>
        </div>
      </div>
    </van-list>

    <van-dialog
      title="你想知道什么？"
      v-model="dialogVisible"
      width="30%"
      @confirm="wantKnow"
      :closeOnClickOverlay="true"
    >
      <van-field v-model="wantKnowValue" placeholder="你想知道什么？"/>
      <span slot="footer" class="dialog-footer"></span>
    </van-dialog>
  </div>
</template>

<script>
import Vue from "vue";
import api from "@/api/index";
import tools from "@/server/storage.server";
import { Button, Tab, Tabs, List, Dialog, Field, Toast, Icon } from "vant";

Vue.use(List)
  .use(Button)
  .use(Dialog)
  .use(Field)
  .use(Toast)
  .use(Tab)
  .use(Tabs)
  .use(Icon);

export default {
  components: {},
  data() {
    return {
      wantKnowValue: "",
      loading: false,
      finished: false,
      INloading: false,
      pages: 1,
      select_trademar: tools.get("home_tra") || 0,
      trademarkList: [],
      docList: [],
      dialogVisible: false
    };
  },
  methods: {
    // ========================================  获取类目列表
    async GetList() {
      const _select_trademar = parseInt(this.select_trademar);
      const { code, data } = await api.getTrademarkList();
      if (code === 0) {
        this.trademarkList = data;
        window.document.title = this.trademarkList[_select_trademar].name;
        this.getDocList();
      }
    },
    // ========================================  获取文章列表
    async getDocList() {
      const _select_trademar = parseInt(this.select_trademar);
      console.log(this.loading, "this.loading");
      if (
        this.finished ||
        this.INloading ||
        !this.trademarkList[_select_trademar]
      ) {
        return;
      }
      const _index = this.trademarkList[_select_trademar]._id;
      const updata = {
        terrace: 1,
        trademark: _index,
        pages: this.pages,
        numbers: 7
      };
      this.INloading = true;
      const { code, data } = await api.getDocList(updata);
      if (code === 0) {
        this.docList = [...this.docList, ...data.list];
        this.loading = false;
        this.INloading = false;
        this.pages++;
        if (this.docList.length >= data.count) {
          this.finished = true;
        }
      }
    },
    // ========================================  想知道
    async wantKnow() {
      const upData = {
        say: this.wantKnowValue
      };
      if (!this.wantKnowValue) {
        return Toast("内容不能为空哦！");
      }
      const { code } = await api.adminWantSay(upData);
      if (code === 0) {
        this.dialogVisible = false;
        Toast("提交成功，感谢你的问题。");
      }
    },
    // ========================================  类目选择
    muneSelect(index) {
      if (!this.trademarkList[index]._id) {
        return;
      }
      this.pages = 1;
      this.docList = [];
      this.finished = false;
      this.loading = false;
      this.select_trademar = index;
      window.document.title = this.trademarkList[index].name;
      tools.set("home_tra", index);
      this.getDocList();
    },
    // ========================================  去详情
    toDetail(id) {
      this.$router.push({
        name: "docDetail",
        params: {
          id
        }
      });
    }
  },
  computed: {},
  mounted: function() {
    this.GetList();
    // this.getTradList();
  }
};
</script>

<style lang="scss" rel="stylesheet/scss" scoped>
.doc-list {
  padding: 10px;
  .item {
    position: relative;
    padding: 5px 0;
    border-bottom: 1px solid #e6e6e6;
  }
  .info {
    min-height: 100px;
    margin-left: 120px;
  }
  .image {
    position: absolute;
    top: 5px;
    width: 100px;
    height: 100px;
    background-size: cover;
    background-position: center center;
    background-repeat: no-repeat;
    background-color: #e1e1e1;
  }
  .infor {
    position: absolute;
    bottom: 0;
    > div {
      display: inline-block;
    }
  }
  .readSize {
    margin-top: 25px;
    color: #bdbdbd;
    font-size: 12px;
  }
  .time {
    text-align: right;
    color: #bdbdbd;
    font-size: 12px;
  }
}

.Uwant {
  margin: 20px;
  margin-bottom: 0;
}

.input-with-select {
  width: 25vw;
}

.pagination {
  margin-top: 10px;
  input {
    height: 30px !important;
  }
}

.docu {
}

.bread {
  font-size: 1.5rem;
}
</style>